package com.sky.sd.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sky.sd.common.web.response.Result;
import com.sky.sd.system.bean.dto.MenuNode;
import com.sky.sd.system.bean.dto.RoleDto;
import com.sky.sd.system.bean.dto.RoleOfMenuDto;
import com.sky.sd.system.bean.po.Menu;
import com.sky.sd.system.bean.po.Role;
import com.sky.sd.system.bean.vo.*;
import com.sky.sd.system.service.IMenuService;
import com.sky.sd.system.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * <p>
 * 系统角色 前端控制器
 * </p>
 *
 * @author 弓振
 * @since 2020-06-30
 */
@RestController
@RequestMapping("/system/role")
@Api(tags = "系统角色模块")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IMenuService menuService;

    @GetMapping("/list")
    @ApiOperation(value = "查询角色信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页", dataType = "int", paramType = "query", example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", example = "10"),
            @ApiImplicitParam(name = "code", value = "角色编码", dataType = "string", paramType = "query", example = ""),
            @ApiImplicitParam(name = "name", value = "角色名称", dataType = "string", paramType = "query", example = "")
    })
    public Result listRole(RoleSearchVo vo) {
        IPage<RoleDto> page = roleService.listRolePage(vo);
        return Result.success(page);
    }

    @GetMapping("/check/code")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "角色编码", dataType = "string", paramType = "query", example = "",required = true)
    })
    @ApiOperation(value = "角色编码是否存在")
    public Result check(RoleCheckVo vo) {
        int cnt = roleService.count(Wrappers.<Role>lambdaQuery()
                .ne(StringUtils.isNotBlank(vo.getId()), Role::getId, vo.getId()).eq(Role::getCode, vo.getCode()));
        return Result.success(cnt);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增角色")
    public Result addRole(@RequestBody Role role) {
        roleService.save(role);
        return Result.success();
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改角色")
    public Result updateRole(@RequestBody Role role) {
        roleService.updateById(role);
        return Result.success();
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除角色")
    public Result deleteRole(@RequestBody DeleteVo vo) {
        Set<String> noDeleteNames = roleService.delete(vo);
        if(CollectionUtils.isNotEmpty(noDeleteNames)) {
            return Result.fail(String.format("角色{%s}已经被关联不能删除",StringUtils.join(noDeleteNames.toArray(), ",")));
        } else {
            return Result.success();
        }
    }

    @GetMapping("/ofMenus")
    @ApiOperation(value = "根据角色id查询菜单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色id", dataType = "string", paramType = "query", example = "",required = true)
    })
    public Result ofMenus(@RequestParam("id") String roleId) {
        List<RoleOfMenuDto> menuIds = roleService.roleOfMenus(roleId);
        return Result.success(menuIds);
    }

    @PostMapping("/toMenus")
    @ApiOperation(value = "角色分配菜单")
    public Result toMenus(@RequestBody RoleToMenusVo vo) {
        roleService.roleToMenus(vo);
        return Result.success();
    }

    @GetMapping("/ofPermsByMenuId")
    @ApiOperation(value = "当前角色关联菜单下的权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色id", dataType = "string", paramType = "query", example = "",required = true),
            @ApiImplicitParam(name = "menuId", value = "菜单id", dataType = "string", paramType = "query", example = "",required = true)
    })
    public Result ofPermsByMenuId(@RequestParam("id") String roleId,@RequestParam("menuId")String menuId) {
        Set<String> permIds = roleService.roleOfPermsByMenuId(roleId, menuId);
        return Result.success(permIds);
    }
    @PostMapping("/toPermsByMenuId")
    @ApiOperation(value = "用户分配权限")
    public Result toPermsByMenuId(@RequestBody RoleToPermsVo vo) {
        roleService.roleToPermsByMenuId(vo);
        return Result.success();
    }
}

